package com.mzp.XbShop.controller;

import com.mzp.XbShop.common.entity.PageResult;
import com.mzp.XbShop.common.entity.Result;
import com.mzp.XbShop.common.entity.StatusCode;
import com.mzp.XbShop.pojo.goodLunbo;
import com.mzp.XbShop.service.GoodLunboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author admin
 * @Description good_lunbo 控制器层
 * @date 2021-04-16 13:45:17
 */
@RestController
@CrossOrigin
@RequestMapping("/goodLunbo")
public class goodLunboController {

    @Autowired
    private GoodLunboService goodLunboService;

    /**
     * 查询全部数据
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll() {
        return new Result(StatusCode.OK, true, "查询成功", goodLunboService.findAll());
    }

    /**
     * 根据ID查询
     *
     * @param id ID
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result findById(@PathVariable String id) {
        return new Result(StatusCode.OK, true, "查询成功", goodLunboService.findById(id));
    }

    /**
     * 分页+多条件查询
     *
     * @param searchMap 查询条件封装
     * @param page      页码
     * @param size      页大小
     * @return 分页结果
     */
    @RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.POST)
    public Result findSearch(@RequestBody Map searchMap, @PathVariable int page, @PathVariable int size) {
        Page<goodLunbo> pageList = goodLunboService.findSearch(searchMap, page, size);
        return new Result(StatusCode.OK, true, "查询成功", new PageResult<goodLunbo>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 根据条件查询
     *
     * @param searchMap
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public Result findSearch(@RequestBody Map searchMap) {
        return new Result(StatusCode.OK, true, "查询成功", goodLunboService.findSearch(searchMap));
    }

    /**
     * 增加
     *
     * @param goodLunbo
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody goodLunbo goodLunbo) {
        goodLunboService.add(goodLunbo);
        return new Result(StatusCode.OK, true, "增加成功");
    }

    /**
     * 修改
     *
     * @param goodLunbo
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public Result update(@RequestBody goodLunbo goodLunbo, @PathVariable String id) {
        goodLunbo.setId(Integer.parseInt(id));
        goodLunboService.update(goodLunbo);
        return new Result(StatusCode.OK, true, "修改成功");
    }

    /**
     * 删除
     *
     * @param id
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable String id) {
        goodLunboService.deleteById(id);
        return new Result(StatusCode.OK, true, "删除成功");
    }
    /**
     * 根据type查找图片
     */
    @GetMapping("/lunbo/{type}")
    public Result lunbo(@PathVariable Integer type) {
        List<goodLunbo> lunboList=goodLunboService.findByType(type);
        System.out.println(lunboList.toString());
        return new Result(StatusCode.OK, true, "查询成功",lunboList);
    }

}
